/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import input.IRCCommand;
import interfaces.BotObject;
import interfaces.CalculableUtopiaEntity;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * A class that represents a building
 * 
 * 
 */
public class Building implements BotObject, CalculableUtopiaEntity {
   private String name = "";
   private final String[] formula;
   private String[] cap;
   private String syntax = "";
   private final String[] result;

   public Building(final String name, final String formula, final String cap, final String syntax, final String result) {
	  this.name = name;
	  this.formula = formula.split("\n");
	  final String[] temp = cap.split("\n");
	  if (temp.length < this.formula.length) { // expands the cap array in case it isn't long enough
		 this.cap = new String[this.formula.length];
		 for (int i = 0; i < this.cap.length; ++i) {
			if (i < temp.length) {
			   this.cap[i] = temp[i];
			} else {
			   this.cap[i] = "0";
			}
		 }
	  } else {
		 this.cap = temp;
	  }
	  this.syntax = syntax;
	  this.result = result.split("\n");
   }

   @Override
   public String[] getCap() {
	  return cap;
   }

   @Override
   public String[] getFormula(final IRCCommand command) {
	  final String[] split = syntax.split(" ");
	  if (split.length != command.size() - 2)
		 return null;
	  final String[] out = new String[formula.length];
	  System.arraycopy(formula, 0, out, 0, formula.length);
	  for (int i = 0; i < split.length; ++i) {
		 for (int j = 0; j < formula.length; ++j) {
			out[j] = out[j].replace(split[i], command.get(i + 2));
		 }
	  }
	  return out;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  // not used
	  return new LinkedList<String>();
   }

   @Override
   public String getName() {
	  return name;
   }

   @Override
   public String[] getResult() {
	  if (result.length != formula.length)
		 return null;
	  return result;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  // not used as of yet
	  return new HashMap<String, Object>();
   }

   @Override
   public boolean saveToDB() {
	  // not used as of yet
	  return false;
   }
}
